<template>
  <div class="job-search">
    <el-card class="box-card">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item label="职位名称" prop="jobName">
            <el-input v-model="searchForm.jobName" placeholder="请输入职位名称"></el-input>
          </el-form-item>
          <el-form-item label="职位类别" prop="categoryId">
            <el-select v-model="searchForm.categoryId" placeholder="请选择职位类别" clearable>
              <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据表格 -->
      <el-table :data="tableData" v-loading="loading" border style="width: 100%">
        <el-table-column prop="jobName" label="职位名称"></el-table-column>
        <el-table-column prop="categoryName" label="职位类别"></el-table-column>
        <el-table-column prop="demandNumber" label="需求人数"></el-table-column>
        <el-table-column prop="hiredNumber" label="已录用人数"></el-table-column>
        <el-table-column prop="remark" label="职位描述" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleApply(scope.row)">申请</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'JobSearch',
  data() {
    return {
      loading: false,
      searchForm: {
        jobName: '',
        categoryId: null
      },
      categoryOptions: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  created() {
    this.fetchCategories()
    this.fetchData()
  },
  methods: {
    async fetchCategories() {
      try {
        const response = await this.$http.get('/job-category/list')
        if (response.data.code === 200) {
          this.categoryOptions = response.data.data
        }
      } catch (error) {
        console.error('获取职位类别失败:', error)
        this.$message.error('获取职位类别失败')
      }
    },
    async fetchData() {
      this.loading = true
      try {
        const response = await this.$http.post('/job/page', {
          ...this.searchForm,
          current: this.currentPage,
          size: this.pageSize
        })
        if (response.data.code === 200) {
          this.tableData = response.data.data.records.map(job => ({
            ...job,
            categoryName: this.getCategoryName(job.categoryId)
          }))
          this.total = response.data.data.total
        }
      } catch (error) {
        console.error('获取职位列表失败:', error)
        this.$message.error('获取职位列表失败')
      } finally {
        this.loading = false
      }
    },
    getCategoryName(categoryId) {
      const category = this.categoryOptions.find(item => item.id === categoryId)
      return category ? category.categoryName : '未知类别'
    },
    async handleApply(row) {
      try {
        await this.$confirm('确认申请该职位?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        const response = await this.$http.post('/job-application/apply', null, {
          params: {
            jobId: row.id,
            seekerId: this.$store.state.user.user.referenceId,
            employerId: row.employerId
          }
        })
        
        if (response.data.code === 200) {
          this.$message.success('申请成功')
          this.fetchData()
        } else {
          this.$message.error(response.data.data || '申请失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('申请失败:', error)
          this.$message.error('申请失败')
        }
      }
    },
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    resetSearch() {
      this.$refs.searchForm.resetFields()
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.job-search {
  padding: 20px;
}
.search-bar {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 